/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import client.ClientView;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import view.DeleteAirportView;
import view.DeleteFlightView;
import view.RegisterAirportView;
import view.RegisterFlightView;
import view.SearchFlightView;

/**
 *
 * @author Tobias
 */
public class Flight {
    private static ResultSet result = null;
    private static CustomTableModel model = null;
    private static ClientView myView = ClientView.getInstance();
    private static CustomTable data = null;
    private static QueryModel query = null;

    public Flight(){    

    }

    public static void showFlights() {
        query = new QueryModel();
        result = query.showFlight();

        // CustomTableModel maintains the result set's data, e.g., if
        // the result set is updatable, it will update the database
        // when the table's data is modified.
        model = new CustomTableModel(query.getConnection(), result);

        data = new CustomTable(model);

        data.setColumnSelectionAllowed(true);
        data.setRowSelectionAllowed(true);

        // Adds the table to the scrollpane.
        // By default, a JTable does not have scroll bars.
        myView.addTable(data);
    }

    public static void addFlight(String origin, String destination, String date, String seats, String price){
            query = new QueryModel();
            query.createFlight(origin, destination, date, seats, price);
    }

    public static void deleteFlight(String id) {
        query = new QueryModel();
        query.deleteFlight(id);
    }

    public static void editFlights() {
        query = new QueryModel();
        result = query.editFlight();
       
        // CustomTableModel maintains the result set's data, e.g., if
        // the result set is updatable, it will update the database
        // when the table's data is modified.
        model = new CustomTableModel(query.getConnection(), result);
        data = new CustomTable(model);
        data.setColumnSelectionAllowed(true);
        data.setRowSelectionAllowed(true);

        // Adds the table to the scrollpane.
        // By default, a JTable does not have scroll bars.
        myView.addTable(data);
    }

    public static ArrayList<String> getFlights() throws SQLException {
        query = new QueryModel();
        ArrayList<String> flights = new ArrayList<String>();
        ResultSet rs = null;

        rs = query.getFlights();
        try {
            while (rs.next()) {
                flights.add(rs.getString(1));
            }
            return flights;
       } catch (SQLException ex) {
            return null;
        }
    }

    public static void addAirport(String name, String tax) {
        query = new QueryModel();
        query.addAirport(name, tax);
    }

    public static void deleteAirport(String name) {
        query = new QueryModel();
        query.deleteAirport(name);
    }

    public static ArrayList<String> getAirports() throws SQLException {
        query = new QueryModel();
        ArrayList<String> airports = new ArrayList<String>();
        ResultSet rs = null;

        rs = query.getAirports();
        try {
            while (rs.next()) {
                airports.add(rs.getString(1));
            }
            return airports;
       } catch (SQLException ex) {
            return null;
        }
    }

    public static void loadRegisterAirportView() throws SQLException {
        ClientView view = ClientView.getInstance();
        RegisterAirportView av = new RegisterAirportView(view);
        av.pack();
        view.centerWindow(av);
        av.setVisible(true);
    }

    public static String getOrigin(String id) throws SQLException {
        String origin = null;
        query = new QueryModel();
        result = query.getFlightOrigin(id);

        while (result.next()) {
            origin = (result.getString(1));
        }
        return origin;
    }
    public static String getDestination(String id) throws SQLException {
        String destination = null;
        query = new QueryModel();
        result = query.getFlightDestination(id);

        while (result.next()) {
            destination= (result.getString(1));
        }
        return destination;
    }

    public static void editAirport() {
        query = new QueryModel();
        result = query.editAirport();

        // CustomTableModel maintains the result set's data, e.g., if
        // the result set is updatable, it will update the database
        // when the table's data is modified.
        model = new CustomTableModel(query.getConnection(), result);
        data = new CustomTable(model);
        data.setColumnSelectionAllowed(true);
        data.setRowSelectionAllowed(true);

        // Adds the table to the scrollpane.
        // By default, a JTable does not have scroll bars.
        myView.addTable(data);
    }

    public static double getFlightPrice(String id) throws SQLException {
        double price = 0;
        query = new QueryModel();
        result = query.getFlightPrice(id);

        while (result.next()) {
            price = (result.getDouble(1));
        }
        return price;
    }

    public static ArrayList<String> getFlightDates() throws SQLException {
        query = new QueryModel();
        ArrayList<String> dates = new ArrayList<String>();
        ResultSet rs = null;

        rs = query.getFlightDates();
        try {
            while (rs.next()) {
                dates.add(rs.getString(1));
            }
            return dates;
       } catch (SQLException ex) {
            return null;
        }
    }

    public static void searchFlight(String origin, String destination, String date) {
        query = new QueryModel();
        result = query.searchFlight(origin, destination, date);

        // CustomTableModel maintains the result set's data, e.g., if
        // the result set is updatable, it will update the database
        // when the table's data is modified.
        model = new CustomTableModel(query.getConnection(), result);

        data = new CustomTable(model);

        data.setColumnSelectionAllowed(true);
        data.setRowSelectionAllowed(true);

        // Adds the table to the scrollpane.
        // By default, a JTable does not have scroll bars.
        myView.addTable(data);
    }

    public static void loadDeleteAirportView() throws SQLException {
        ClientView view = ClientView.getInstance();
        DeleteAirportView av = new DeleteAirportView(view);
        av.pack();
        view.centerWindow(av);
        av.setVisible(true);
    }


    public static void loadRegisterFlightView() throws SQLException {
        ClientView view = ClientView.getInstance();
        RegisterFlightView rf = new RegisterFlightView(view);
        rf.pack();
        view.centerWindow(rf);
        rf.setVisible(true);
    }

    public static void loadDeleteFlightView() throws SQLException {
        ClientView view = ClientView.getInstance();
        DeleteFlightView df = new DeleteFlightView(view);
        df.pack();
        view.centerWindow(df);
        df.setVisible(true);
    }

    public static void loadSearchFlightView() throws SQLException {
        ClientView view = ClientView.getInstance();
        SearchFlightView sv = new SearchFlightView(view);
        sv.pack();
        view.centerWindow(sv);
        sv.setVisible(true);
    }
}
